//Eli Elder
//steward
//makefile needs this
//cardtest3: cardtest3.c dominion.o rngs.o
//	gss -o cardtest3 -g cardtest3.c dominion.o rngs.o $(CFLAGS)


#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h> 
#include <assert.h>
#include <math.h>
#include "rngs.h"

//set PRINTOUT to 0 to remove printfs out
#define PRINTOUT 1
#define ASSERTIVE 0

int checkSteward(int card, int choice1, int choice2, int choice3, struct gameState *afterStewardState, int handPos, int *bonus){
    struct gameState beforeStewardState;
    int returnVal;
    
    int player = whoseTurn(afterStewardState);
    //copy so we can test if the steward we can check
    memcpy(&beforeStewardState, afterStewardState, sizeof(struct gameState));
    //do the steward function card
    returnVal = cardEffect(steward, choice1, choice2, choice3, afterStewardState, handPos, 0);
    //choice 1 as 1 is to Steward 2 card
    if(choice1 == 1){
	drawCard(player, &beforeStewardState);
	drawCard(player, &beforeStewardState);
    } else if(choice1 == 2){
	//choice 1 as 2 is gain 2 coins
	beforeStewardState.coins = beforeStewardState.coins + 2;
    } else {
	//last choice is to discard 2 cards specified by choice2 and 3
	discardCard(choice2, player, &beforeStewardState, 1);
	discardCard(choice3, player, &beforeStewardState, 1);
    }
    
    //now we discard the card we just played
    discardCard(handPos, player, &beforeStewardState, 0);
    
#if (PRINTOUT == 1)
    //if the full deck counts arent the same gain and discard messed up
    if(fullDeckCount(player, 0, &beforeStewardState) < fullDeckCount(player, 0, afterStewardState)){
	printf("The steward state has more cards than the test\n");
    } else if(fullDeckCount(player, 0, &beforeStewardState) > fullDeckCount(player, 0, afterStewardState)){
		printf("The steward state has less cards than the test\n");
    }
    
    //check that coins are correct just in case they chose that
    if(beforeStewardState.coins < afterStewardState->coins){
	printf("The steward state has more coins than the test\n");
    } else if(beforeStewardState.coins > afterStewardState->coins){
	printf("The steward state has less coins than the test\n");
    }
    
    //check the hand cards
    if(numHandCards(&beforeStewardState) < numHandCards(afterStewardState)){
	printf("The Stewardcard has more hand cards than the test\n");
    } else if(numHandCards(&beforeStewardState) > numHandCards(afterStewardState)){
	printf("The Stewardcard has less hand cards than the test\n");
    }
#endif
#if (ASSERTIVE == 1)
    //this will assert if something went wrong, those ifs above tell us what went wrong
    assert(returnVal == 0);
    //assert that the after StewardCard state is the same as we expect it to be with the "before" that we made like the after
    assert(memcmp(&beforeStewardState, afterStewardState, sizeof(struct gameState)) == 0);
#endif

    return 0;
}

int main(){
    int i;
    int player = 1;
    int bonus = 0;
    int numDeck = 5;
    int numDiscard = 5;
    int numHand = 5;
    int initialized;
    int coppers[MAX_DECK];
    struct gameState theGameState;
    int kingCards[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, salvager};


    for(i = 0; i < MAX_DECK; i++){
        coppers[i] = copper;
    }

    //setup and initialize the state                                                                                                                                              
    memset(&theGameState, 23, sizeof(struct gameState));
    initialized = initializeGame(player, kingCards, 1, &theGameState);    

    //assign the deck, hand, and discard
    theGameState.discardCount[player] = numDiscard;
    theGameState.handCount[player] = numHand;
    theGameState.deckCount[player] = numDeck;

    //mem set the deck hand and discard                                                                                                                                                                                                                                                                                                                             
    memcpy(theGameState.discard[player], coppers, sizeof(int) * MAX_DECK);
    memcpy(theGameState.hand[player], coppers, sizeof(int) * MAX_HAND);
    memcpy(theGameState.deck[player], coppers, sizeof(int) * MAX_HAND);

    theGameState.whoseTurn = player;
    //check steward                                                                                                                                                                

    theGameState.playedCards[1] = 1;
    theGameState.playedCardCount=0;
    theGameState.hand[player][silver] = steward;
    checkSteward(steward, 1, minion, gold, &theGameState, silver, &bonus);
    printf("drew two cards\n");

    theGameState.playedCards[1] = 1;
    theGameState.playedCardCount=0;
    theGameState.hand[player][silver] = smithy;
    checkSteward(steward, 2 , mine, gold, &theGameState, silver, &bonus);
    printf("gained two coins\n");

    theGameState.playedCards[1] = 1;
    theGameState.playedCardCount=0;
    theGameState.hand[player][silver] = smithy;
    checkSteward(steward, 3 , mine, gold, &theGameState, silver, &bonus);
    printf("trashed 2 cards\n");

	printf("Note, if ASSERT and PRINTOUT are not flagged everything might look fine, but not be fine\n");
    return 0;
}
